package com.zekdot.registerclient.dao;

import com.zekdot.registerclient.entity.EnrollDataEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Create by zekdot on 20-3-22.
 */
@Mapper
@Repository
public interface EnrollDataDao {

    /**
     * 存储一个报名数据
     * @param enrollDataEntity 报名数据实体
     * @return
     */
    int save(EnrollDataEntity enrollDataEntity);

    /**
     * 更新存储
     * @param enrollDataEntity 报名数据实体
     * @return
     */
    int update(EnrollDataEntity enrollDataEntity);
    /**
     * 存储多个报名数据
     * @param enrollDataEntities 报名数据列表
     * @return
     */
    int saves(@Param("enrollDataEntities") List<EnrollDataEntity> enrollDataEntities);
    /**
     * 获取某个报名的所有报名数据
     * @param EId 报名id
     * @return
     */
    @Select("SELECT * FROM enroll_data WHERE e_id=#{EId}")
    List<EnrollDataEntity> getDataByEid(@Param("EId") String EId);

    /**
     * 获取某个报名的所有报名数据描述
     * @param eId 报名id
     * @param username 用户名
     * @param lastTime 最后一条提交的时间
     * @param length 长度
     * @return
     */
    @Select("SELECT d.username,d.submit_time,d.check_time,d.status FROM enroll_data d,enroll_meta m WHERE d.e_id=#{eId} " +
            "AND d.e_id=m.id AND m.host=#{username} AND d.submit_time<#{lastTime} ORDER BY d.submit_time DESC LIMIT #{length}")
    List<EnrollDataEntity> getDataDescsByEid(@Param("eId") String eId,@Param("username") String username,
                                            @Param("lastTime")Date lastTime,@Param("length")int length);

    /**
     * 更新某个报名数据的状态
     * @param eId 报名id
     * @param userId 用户id
     * @param username 用户名
     * @param status 状态
     * @param rejectReason 拒绝原因
     * @return
     */
    @Update("UPDATE enroll_data d,enroll_meta m SET d.status=#{status},d.check_time=NOW(),d.reject_reason=#{rejectReason} " +
            "WHERE d.username=#{username} AND d.e_id=#{eId} AND d.e_id=m.id AND m.host=#{userId}")
    int updateStatus(@Param("eId") String eId,@Param("userId")String userId,@Param("username") String username,
                     @Param("status") int status,@Param("rejectReason")String rejectReason);

    /**
     * 返回对应报名的结果
     * @param eId 报名id
     * @param username 用户名
     * @return
     */
    @Select("SELECT d.content,d.submit_time,d.check_time,d.status,d.reject_reason,m.status_tip FROM enroll_data d,enroll_meta m WHERE d.e_id=m.id AND d.e_id=#{eId} AND username=#{username};")
    Map<String,Object> getEnrollResult(@Param("eId") String eId, @Param("username") String username);

    /**
     * 获取填写过的报名列表
     * @param username 用户名
     * @return
     */
    @Select("SELECT m.title,d.e_id eId,d.submit_time submitTime,d.check_time checkTime,d.status FROM enroll_data d,enroll_meta m " +
            "WHERE username=#{username} AND submit_time<#{lastTime} AND m.id=d.e_id ORDER BY submit_time " +
            "DESC LIMIT #{length}")
    List<Map<String,Object>> moreEnrollData(@Param("username")String username, @Param("lastTime") Date lastTime,
                                            @Param("length") int length);

    /**
     * 获得报名数据详情
     * @param userId 当前用户id
     * @param username 要查的用户名
     * @param eId 报名id
     * @return
     */
    @Select("SELECT d.content,d.submit_time,d.check_time,d.status,d.reject_reason FROM enroll_data d,enroll_meta m " +
            "WHERE d.e_id=m.id AND m.host=#{userId} AND d.username=#{username} AND d.e_id=#{eId}")
    EnrollDataEntity enrollDataDetail(@Param("userId")String userId,@Param("username")String username,
                                      @Param("eId")String eId);

    /**
     * 获取该报名当前的人数，只计算审核通过的人数
     * @param eId
     * @return
     */
    @Select("SELECT COUNT(e_id) FROM enroll_data WHERE e_id=#{eId} AND status=1")
    int getEnrollDataCount(String eId);

    /**
     * 获取某一个报名的状态
     * @param userId
     * @param eId
     * @return
     */
    @Select("SELECT status FROM enroll_data WHERE username=#{userId} AND e_id=#{eId}")
    EnrollDataEntity getEnrollStatus(@Param("userId")String userId,@Param("eId")String eId);

    /**
     * 根据token和报名id获取报名的未审核的所有用户
     * @param eId 报名id
     * @param token 操作token
     * @param length 长度
     * @return
     */
    @Select("SELECT u.username,u.name,u.intro,u.identify,u.join_met,u.join_time,d.content,d.submit_time FROM enroll_data d,enroll_token t,user u WHERE t.token=#{token}" +
            "AND t.e_id=#{eId} AND t.e_id=d.e_id AND d.username=u.username AND d.status=0 ORDER BY d.submit_time ASC LIMIT #{length}")
    List<Map<String,Object>> getEnrollDataByToken(@Param("eId") String eId,@Param("token") String token,@Param("length") int length);

    /**
     * 获取所有没有审核过的用户名
     * @param eId 报名id
     * @return
     */
    @Select("SELECT username FROM enroll_data WHERE e_id=#{eId} AND status=0")
    List<String> getUncheckedUsername(@Param("eId")String eId);

    /**
     * 批量更新状态
     * @param eId 报名id
     * @param reqs 请求列表
     * @return
     */
    int batchUpdateStatus(@Param("eId") String eId,@Param("reqs") List<Map<String,Object>> reqs);

    /**
     * 验证令牌的正确性
     * @param eId 报名id
     * @param token 令牌
     * @return
     */
    @Select("SELECT count(e_id) FROM enroll_token WHERE e_id=#{eId} AND token=#{token}")
    int validateToken(@Param("eId") String eId,@Param("token") String token);


    /**
     * 根据报名id和用户id获取所有通过报名的数据
     * @param eId 报名id
     * @return
     */
    @Select("SELECT username,submit_time,content FROM enroll_data WHERE e_id=#{eId} AND status=1")
    List<EnrollDataEntity> getExcelData(@Param("eId") String eId);

    /**
     * 获取可重用的用户
     * @param eId 报名id
     * @return
     */
    @Select("SELECT u.username,u.name,u.join_met joinMet,u.join_time joinTime,u.intro,u.identify" +
            " FROM user u,enroll_data e WHERE e.e_id=#{eId} AND (e.status=1 OR e.status=3) AND u.username = e.username")
    List<Map<String,Object>> getReuseUser(@Param("eId") String eId);
}
